package com.qingxin.shebei.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.shebei.entity.Shebei;
import com.qingxin.shebei.service.ShebeiService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;

/**
 * 设备管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/shebei")
public class ShebeiAction extends BaseAction {
	
	@Autowired
	private ShebeiService shebeiService;
	
	/**
	 * 列表页
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/shebei/shebei_list";
	}
	
	/**
	 * 获取列表分页数据
	 * @param parkid
	 * @param name
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json" , method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(@RequestParam(value="leftdata")String parkid,String searchName, PageParam pageParam, HttpServletRequest request){
		return shebeiService.list(parkid, searchName,pageParam, getLoginUser(request));
	}
	
	
	/**
	 * 添加设备信息
	 * @param model
	 * @param parkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, @RequestParam(value="leftdata")String parkid, HttpServletRequest request){
		model.put("parkid", parkid);
//		FarmPark park = farmParkService.get(parkid);
//		model.put("park", park);
		
		return "trace/shebei/shebei_add";
	}
	
	
	/**
	 * 保存设备信息
	 * @param shebei
	 * @param result
	 * @param parkid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid Shebei shebei, BindingResult result, String parkid, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = shebeiService.add(shebei, parkid, getLoginUser(request), request);
		return info;
	}

	
	/**
	 * 修改设备信息
	 * @param model
	 * @param sheid
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String sheid){
		Shebei shebei = shebeiService.get(sheid);
		model.put("shebei", shebei);
		
		//FarmPark park = farmParkService.get(shebei.getFarmPark().getParkid());
		//model.put("park", park);
		
		return "trace/shebei/shebei_edit";
	}
	
	
	/**
	 * 保存修改信息
	 * @param shebei
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid Shebei shebei, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = shebeiService.update(shebei, getLoginUser(request), request);
		return info;
	}
	
	
	/**
	 * 查看设备详情
	 * @param model
	 * @param sheid
	 * @return
	 */
	@RequestMapping(value="/view.do", method=RequestMethod.GET)
	public String view(ModelMap model, String sheid){
		Shebei Shebei = shebeiService.get(sheid);
		model.put("Shebei", Shebei);
		
//		FarmPark park = farmParkService.get(Shebei.getFarmPark().getParkid());
//		model.put("park", park);
		
		return "trace/shebei/shebei_view";
	}
	
	
	
	/**
	 * 删除设备信息
	 * @param sheid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] sheid, HttpServletRequest request){
		if(sheid==null || sheid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return shebeiService.delete(sheid, getLoginUser(request), request);
	}
	
}
